<template>
  <el-card class="swiper-container">
    <template #header>
      <!-- 补充查询条件 -->
      <div style="display: flex;flex-direction: row;">
        <el-form :inline="true" :model="queryForm" class="demo-form-inline">
          <el-form-item label="用户名">
            <el-input v-model="queryForm.userName" placeholder="用户名"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input v-model="queryForm.userPhone" placeholder="手机号"/>
          </el-form-item>
        </el-form>

        <div class="header">
          <el-button type="primary" :icon="Plus" @click="getCarousels">查询</el-button>
          <el-button type="primary" :icon="Plus" @click="handleAdd">增加</el-button>
          <el-popconfirm
              title="确定删除吗？"
              confirmButtonText='确定'
              cancelButtonText='取消'
              @confirm="handleDelete"
          >
          </el-popconfirm>
        </div>
      </div>

    </template>
    <el-table
        :load="state.loading"
        ref="multipleTable"
        :data="state.tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange"
    >
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="userName"
          label="用户名"
          width="200">
      </el-table-column>
      <el-table-column
          prop="userPhone"
          label="用户手机号"
      >
      </el-table-column>
      <el-table-column
          prop="userMoney"
          label="用户余额"
      >
      </el-table-column>
      <el-table-column
          prop="userPoint"
          label="用户积分"
      >
      </el-table-column>
      <el-table-column
          label="用户角色"
      >
        <template #default="scope">
          <!--<p>{{scope.row.roleIds}}</p>-->
          <div style="display: flex;flex-direction: row">
            <template v-for="item in scope.row.userRoles">
              <el-tag class="ml-2" v-if="item.roleId in [1,2,3,4]" type="danger">{{ item.roleName }}</el-tag>
              <el-tag class="ml-2" v-else type="info">{{ item.roleName }}</el-tag>
            </template>
          </div>
        </template>
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="添加时间"
          width="200"
      >
      </el-table-column>
      <el-table-column
          label="操作"
          width="200"
      >
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          <el-button type="danger" @click="deleteOne(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        background
        layout="prev, pager, next"
        :total="state.total"
        :page-size="state.pageSize"
        :current-page="state.currentPage"
        :page-count="state.pages"
        @current-change="changePage"
    />
  </el-card>
  <DialogAddUser ref='addUser' :reload="getCarousels" :type="state.type"/>
</template>

<script setup>
import {onMounted, reactive, ref, toRefs} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {Plus, Delete} from '@element-plus/icons-vue';
import axios from '@/utils/axios';
import DialogAddUser from '@/components/sysComp/DialogAddUser.vue';

import {userList, userDelete} from '@/api/user';
import {useLoginStore} from '@/store/login.js';

const store = useLoginStore();


const addUser = ref();

const state = reactive({
  loading: false,
  tableData: [], // 数据列表
  currentPage: 1,
  pageSize: 10,
  pages: 1,
  type: 'add', // 操作类型,
  multipleSelection: [], // 选中项
  total: 0 // 总条数
});

onMounted(() => {
  getCarousels();
});
// 获取轮播图列表
const getCarousels = async () => {
  state.loading = true;
  await userList(
      {
        userName: queryForm.value.userName,
        userPhone: queryForm.value.userPhone,
        pageNumber: state.currentPage,
        pageSize: state.pageSize

      }).then(res => {
    console.log('当前查询结果为=>', res);
    state.tableData = res.data.data.records;
    state.total = res.data.data.pages;
    state.currentPage = res.data.data.current;
    state.pages = res.data.data.pages;
    state.loading = false;
  });
  console.log('state.tableData', state.tableData);
};
// 添加用户
const handleAdd = () => {
  // console.log('addSwiper', addSwiper.value)
  console.log('addUser', addUser.value);
  state.type = 'add';
  addUser.value.open();
  // addSwiper.value.open()
};
// 修改轮播图
const handleEdit = (index, row) => {
  state.type = 'edit';
  // addSwiper.value.open(id)
  console.log('修改用户', addUser.value);
  addUser.value.open(row);
};
// 选中之后的change方法，一旦选项有变化，就会触发该方法。
const handleSelectionChange = (val) => {
  state.multipleSelection = val;
};
// 批量删除
const handleDelete = () => {
  if (!state.multipleSelection.length) {
    ElMessage.error('请选择项');
    return;
  }
  axios.delete('/carousels', {
    data: {
      ids: state.multipleSelection.map(i => i.carouselId)
    }
  }).then(() => {
    ElMessage.success('删除成功');
    getCarousels();
  });
};
// 单个删除
const deleteOne = (index, row) => {
  console.log('删除用户', index);
  console.log('删除用户', row);
  console.log(store.loginUserInfo);
  if (store.loginUserInfo.id === row.id) {
    return ElMessage.error('不能删除自己');
  }
  ElMessageBox.confirm(
      '确认删除？'
  ).then(() => {
    userDelete(row.id).then(() => {
      ElMessage.success('删除成功');
      getCarousels();
    }).error((e) => {
      ElMessage.error('删除失败！' + e.message);
    });
  });
};

const queryForm = ref({
  userName: '',
  userPhone: ''
})

const changePage = (val) => {
  state.currentPage = val;
  getCarousels();
};
</script>

<style>

</style>
